﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Atom.Tool
{
    /// <summary>
    /// 单线程处理
    /// 多线程里面进行单线程调用
    /// </summary>
    public delegate void ExecutorDelegata();
    public class ExecutorPool
    {
        private static ExecutorPool instance;

        /// <summary>
        /// 
        /// </summary>
        private Mutex tex = new Mutex();

        public static ExecutorPool Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ExecutorPool();
                }
                return instance;
            }
        }


        public void Executor(ExecutorDelegata e)
        {
            lock (this)
            {
                //阻止当前线程，等待信号，重开线程
                tex.WaitOne();
                e();
                //发送信号，重开线程
                tex.ReleaseMutex();
            }
        }
    }
}
